#include "Avalon/IO/DelimitedWriter.hpp"
#include "Avalon/IO/Buffer.hpp"

using namespace Avalon;
using namespace Avalon::IO;
using namespace Avalon::Threading;

DelimitedWriter::DelimitedWriter(Carry<Writer> destination, char delimiter)
    : m_destination(destination),
      m_delimiter(delimiter) {}

DelimitedWriter::~DelimitedWriter() {}

void DelimitedWriter::Write(const void* data, int size,
    Out<Async<void> > result) {
  Buffer coalescedData(size + sizeof(m_delimiter));
  coalescedData.Write(0, data, size);
  coalescedData.Write(size, &m_delimiter, sizeof(m_delimiter));
  m_destination->Write(coalescedData, Store(result));
}

void DelimitedWriter::Write(const Buffer& data, Out<Async<void> > result) {
  Write(data.GetData(), data.GetSize(), Store(result));
}
